/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.escom.info.scanner.jsf;

import com.escom.administracion.dao.AlumnoDAO;
import com.escom.administracionperfiles.correo.ProcesoJMailDAO;
import com.escom.administracionperfiles.hbm.dto.Alumno;
import com.escom.cliente.exception.DAOException;
import com.escom.info.scanner.jsf.util.JsfUtil;
import java.io.Serializable;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;


/**
 *
 * @author yamil
 */
public class AlumnoController  implements Serializable{

    private AlumnoDAO ejbFacade;
    private Alumno current;
    private List<Alumno> listaAlumnos;
    private ProcesoJMailDAO   correoDAO;
    private String confirmaCorreo;


    public String getConfirmaCorreo() {
        return confirmaCorreo;
    }

    public void setConfirmaCorreo(String confirmaCorreo) {
        this.confirmaCorreo = confirmaCorreo;
    }


    public ProcesoJMailDAO getCorreoDAO() {
        return correoDAO;
    }

    public void setCorreoDAO(ProcesoJMailDAO correoDAO) {
        this.correoDAO = correoDAO;
    }
public AlumnoController() {
    }





    public Alumno getSelected() {
        if(current==null){
            current=new Alumno();

        }
        return current;
    }

    public void setCurrent(Alumno current) {
        this.current = current;
    }


    private AlumnoDAO getEjbFacade() {
        return ejbFacade;
    }

    public void setEjbFacade(AlumnoDAO ejbFacade) {
        this.ejbFacade = ejbFacade;
    }


    public String prepareCreate(){
        current=new Alumno();
        return "Create";
    }

    public List<Alumno> getListaAlumnos(){
        try
        {
            listaAlumnos = getEjbFacade().findAll();
        } catch (DAOException ex) {
            Logger.getLogger(AlumnoController.class.getName()).log(Level.SEVERE, null, ex);
        }
        return listaAlumnos;
    }

    public String create(){
        if(current.getCorreo().equals(getConfirmaCorreo())){

        try 
        {
          if(getEjbFacade().findAlumnoByBoletaAndCorreo(current.getBoleta(), current.getCorreo()))
           {
              JsfUtil.addErrorMessage("Ya existe un Alumno con esta boleta y correo electronico");
              return null;
           }

           current.setApmaterno(current.getApmaterno());
           current.setAppaterno(current.getAppaterno());
           current.setBoleta(current.getBoleta());
           current.setCorreo(current.getCorreo());
           current.setContrasenia(current.getContrasenia());
           setConfirmaCorreo("");
           getEjbFacade().create(current);
           sendEnviarCorreo();
           JsfUtil.addSuccessMessage("Alumno fue creado Satisfactoriamente");
        } catch (DAOException ex) {
            Logger.getLogger(AlumnoController.class.getName()).log(Level.SEVERE, null, ex);
            JsfUtil.addErrorMessage("Error el alumno no ha sido creado"); //AQUI ESTA
        }
        }else{
            JsfUtil.addErrorMessage("Error los correos no son iguales");
            setConfirmaCorreo("");
            return null;
        }
        return "View";
    }

    private void sendEnviarCorreo(){
       
        try
        {
           correoDAO.enviarCorrreoRegistro(current.getCorreo(), String.valueOf(current.getBoleta()), current.getContrasenia(),"MAC");
           JsfUtil.addSuccessMessage("Mensaje Enviado Exitosamente");
        } catch (DAOException ex) {
            Logger.getLogger(AlumnoController.class.getName()).log(Level.SEVERE, null, ex);
            JsfUtil.addErrorMessage("Error al enviar correo electronico");
        }
    }

    public String update(){
          try {
            if(!getConfirmaCorreo().equals(current.getCorreo()))
            {
              JsfUtil.addErrorMessage("Error correo electronico son distintos");
              return null;
            }
           current.setApmaterno(current.getApmaterno());
           current.setAppaterno(current.getAppaterno());
           current.setBoleta(current.getBoleta());
           current.setCorreo(current.getCorreo());
           current.setContrasenia(current.getContrasenia());
           getEjbFacade().edit(current);
            JsfUtil.addSuccessMessage("Alumno actualizado Satisfactoriamente");
        } catch (Exception ex) {
            Logger.getLogger(AlumnoController.class.getName()).log(Level.SEVERE, null, ex);
            JsfUtil.addErrorMessage("Error el alumno no ha sido creado"); //AQUI ESTA
        }
          return "View";
    }

    public String prepareEdit(Alumno dominio){
        current=dominio;
        setConfirmaCorreo(current.getCorreo());
        return "Edit";
    }

    public String prepareView(Alumno dominio){
        current=dominio;
        return "View";
    }

    public void prepareDestroy(Alumno dominio){
        try {
            //current=dominio;
            getEjbFacade().remove(dominio);
            JsfUtil.addSuccessMessage("Alumno eliminado Satisfactoriamente");
        } catch (DAOException ex) {
            Logger.getLogger(AlumnoController.class.getName()).log(Level.SEVERE, null, ex);
            JsfUtil.addErrorMessage("Error el alumno no ha sido eliminado"); //AQUI ESTA
        }
    }

    public String prepareList(){
        return "List";
    }

     @FacesConverter(forClass=Alumno.class)
    public static class AlumnoControllerConverter implements Converter {

        @Override
        public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
           try{
                if (value == null || value.length() == 0) {
                    return null;
                }
                AlumnoController controller = (AlumnoController) facesContext.getApplication().getELResolver().
                getValue(facesContext.getELContext(), null, "alumnoController");
                return controller.getEjbFacade().find(getKey(value));
           }catch(Exception e){
               e.printStackTrace();
           }
           return null;

        }

        java.lang.String getKey(String value) {
            java.lang.String key;
            key = value;
            return key;
        }

        String getStringKey(java.lang.Integer value) {
            StringBuffer sb = new StringBuffer();
            sb.append(value);
            return sb.toString();
        }
        
        @Override
        public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
            if (object == null) {
                return null;
            }
            if (object instanceof Alumno) {
                Alumno o = (Alumno) object;
                return getStringKey(o.getBoleta());
            } else {
                throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: "+UsuariosController.class.getName());
            }
        }



        

    }

}
